package com.example.demospringmybatisplus.handler;

import com.example.demospringmybatisplus.vo.NodeVo;

import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 自定义刷新网元拒绝处理策略
 *
 * @author huahua
 * @DATE 2025/5/1
 **/
@Component
public class RefreshNodeRejectHandler implements RejectedExecutionHandler {

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        // 拒绝将任务保存起来
        if (!executor.isShutdown()) {
            saveRejectedTask(r);
        }
    }

    class TaskInfo implements Runnable {
        private final List<NodeVo> nodeVos;

        TaskInfo(List<NodeVo> nodeVos) {
            this.nodeVos = nodeVos;
        }

        @Override
        public void run() {
            System.out.println("执行了TaskInfo的run方法，执行了某某业务逻辑");
        }

        public List<NodeVo> getNodeVos() {
            return nodeVos;
        }
    }

    private void saveRejectedTask(Runnable r) {
        if (r instanceof TaskInfo) {
            TaskInfo taskInfo = (TaskInfo) r;
            List<NodeVo> nodeVos = taskInfo.getNodeVos();
            for (NodeVo node : nodeVos) {
                System.out.println("保存了被拒绝的任务：" + node);
            }
        }
    }
}
